<?php
/**
 * 管理员模块
 **/
namespace Mama\Controller;
use Think\Controller;

class AccessController extends Controller {
	public function __construct() {
        parent::__construct();
		//判断是否登陆
		if(!session('userid')){ //已登陆
			$this->redirect("index/index");
		}

		//传递参数到分页界面
		$this->assign('param', json_encode(I('get.')));
    }

    /**
     * 列表
     **/
    public function index(){
    	if(IS_GET){
			$ajax = I('get.ajax', 0);
			$page = I('get.page', 1);

			$where = array();

			$accessDao = D('Access');
			//获取总数
			$count = $accessDao->getAccessCount($where);
			$this->assign('count', $count);
			//获取列表
			$accesslist = $accessDao->getAccessList($page, $where);
			$this->assign('accesslist', $accesslist);
			$this->assign('page', $page);
			if($ajax){
				$this->display('access_list', 'utf-8', 'html');
			}else{
				$this->display();
			}
		}
    }

    /**
     * 物品出租列表
     **/
    public function matching(){
        if(IS_GET){
            $ajax = I('get.ajax', 0);
            $page = I('get.page', 1);
            $where = array();
            $accessid = I('get.aid', 0);
            if(!empty($accessid)){
                $where['access_id'] = $accessid;
            }

            $accessDao = D('Access');
            //获取类型列表
            $alist = $accessDao->getAllAccessList();
            $this->assign('alist', $alist);
            //获取总数
            $count = $accessDao->getAccessMatchingCount($where);
            $this->assign('count', $count);
            //获取列表
            $accesslist = $accessDao->getAccessMatchingList($page, $where);
            $this->assign('accesslist', $accesslist);
            $this->assign('page', $page);
            if($ajax){
                $this->display('match_list', 'utf-8', 'html');
            }else{
                $this->display();
            }
        }
    }

    /**
     * 添加与编辑配件
     **/
    public function add(){
    	if(IS_GET){
    		$accessid = I('get.aid', 0);
    		if(!empty($accessid)){
	    		$info = D('Access')->getAccessInfoById($accessid);
    			$this->assign('info', $info);
    		}

    		$this->display();
    	}else{
    		$data = I('post.');
    		$data['createtime'] = time();
    		$data['admin_id'] = session('userid');
    		$data['admin_nick'] = session('nickname');
    		$accessid = $data['accessid'];
    		unset($data['accessid']);
    		if($accessid){
    			$row = D('Access')->editAccessInfoByAccessId($accessid, $data);
    		}else{
    			$row = D('Access')->addAccessInfo($data);
    		}

    		if($row !== false){
    			echo 1;
    		}else{
    			echo 0;
    		}
    	}
    }

    /**
     * 编辑状态
     **/
    public function setStatus(){
    	$access_id = I('post.userid', 0);
    	$status = I('post.visible', 0);

    	$row = D('Access')->editAccessInfoByAccessId($access_id, array(
    		'visible' => $status
    	));

    	if($row !== false){
    		echo 1;
    	}else{
    		echo 0;
    	}
    }
}    